import re
import json
import csv
import pickle
from datetime import datetime
import dicttoxml

from abc import ABC, abstractmethod



class Student:
    def __init__(self, student_id, name, gender, ethnicity,calss_info):
        self.student_id = student_id
        self.name = name
        self.gender = gender
        self.ethnicity = ethnicity
        self.calss_info = calss_info
        self._dormitory_id = None

    def to_dict(self):
        return {
            "student_id": self.student_id,
            "name": self.name,
            "gender": self.gender,
            "ethnicity": self.ethnicity,

            "dormitory_id": self._dormitory_id
        }



class Dormitory:
    def __init__(self, dormitory_id, capacity, gender=0):
        self.dormitory_id = dormitory_id
        self.capacity = capacity
        self.gender = gender
        self._students = []

    def add_student(self, student):
        if len(self._students) >= self.capacity:
            raise Exception("宿舍已满！")
        if self.gender != 0 and self.gender != student.gender:
            raise Exception("性别不符合宿舍规则！")
        else:
            self._students.append(student)

    def remove_student(self, student):
        if student in self._students:
            self._students.remove(student)
            student._dormitory_id = None
            print("{}同学已经从{}宿舍移除".format(student.name, student.dormitory_id))
            return True

    def is_full(self):
        return len(self._students) >= self.capacity

    def check_gender_rule(self, student):
        return self.gender == 0 or self.gender == student.gender

    def check_ethnicity_rule(self):
        ethnicities = set(s.ethnicity for s in self._students)
        return len(ethnicities) > 1

class Teacher():
    def __init__(self, teacher_id, name):
        super().__init__(teacher_id, name)


    def query_dormitory(self, dormitories, dormitory_id):
        for d in dormitories:
            if d.dormitory_id == dormitory_id:
                return d._students
        return []

    def login(self):
        tercher_name=input("请输入教师姓名")
        print("1")

class admin():
    admin1=input("请输入姓名,id(空格隔开)").split()

    def add_student(self, student,dormitories,log_file):
        while True:
            name=input("学生姓名：")
            student_id=input("学号（4位数字）")
            if not re.match(r'^\d{4}$',student_id):
                print("学号必须是4位数字，请重新输入")
                continue
            gender=input("性别：1男，2女")
            if gender =="1":
                gender="男"
            elif gender =="2":
                gender="女"
            else:
                print("输入无效，请重新输入")
                continue
            ethnicity_choice =input("民族：1汉 2维吾尔 3 哈萨克 4藏族 5其它")
            ethnicities={'1':"汉","2":"维吾尔","3":"哈萨克","4":"藏族", "5":"其它"}
            ethnicity=ethnicities[ethnicity_choice]
            class_info=input("班级：")
            student=Student(name,student_id,gender,ethnicity,class_info)






